﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BestCode.Creds.Data;
using BestCode.Creds.Entities;
using BestCode.Wpf.ViewModel.Data.Contracts;

namespace BestCode.Creds.Business
{
    public class BusinessBase<T> : IDataModelBase<T> where T : EntityId
    {
        private IDataAccessBase<T> dataAccess;
        
        public void setDao(IDataAccessBase<T> dao)
        {
            dataAccess = dao;
        }

        
        public virtual void PostInsert(T entity) { }

        public virtual void PostSave(T entity) { }

        public T Save(T entity)
        {
            if (!entity.IsAssigned)
            {
                dataAccess.Insert(entity);
                PostInsert(entity);
            }
            else
            {
                dataAccess.Update(entity);    
            }

            PostSave(entity);
            return entity;
        }

        public void Delete(T entity)
        {
            if(entity.IsAssigned)
                dataAccess.Delete(entity);
        }

        public ObservableCollection<T> GetAll()
        {
            return new ObservableCollection<T>(dataAccess.GetAll());
        }

        public T Get(object id)
        {
            return dataAccess.GetById(id);
        }

        public virtual ObservableCollection<T> SearchByFilter(string parameter)
        {
            return new ObservableCollection<T>();
        }
    }
}
